package com.tinycloud.miracll.product.service.impl;

import java.util.List;

import com.tinycloud.common.datasource.annotation.Slave1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.tinycloud.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.tinycloud.miracll.product.domain.ProductInstructionDetail;
import com.tinycloud.miracll.product.mapper.ProductInstructionMainMapper;
import com.tinycloud.miracll.product.domain.ProductInstructionMain;
import com.tinycloud.miracll.product.service.IProductInstructionMainService;

/**
 * 生产指令Service业务层处理
 * 
 * @author tiny
 * @date 2022-05-25
 */
@Service
@Slave1
public class ProductInstructionMainServiceImpl implements IProductInstructionMainService {
    @Autowired
    private ProductInstructionMainMapper productInstructionMainMapper;

    /**
     * 查询生产指令
     * 
     * @param instNum 生产指令主键
     * @return 生产指令
     */
    @Override
    public ProductInstructionMain selectProductInstructionMainByInstNum(String instNum) {
        return productInstructionMainMapper.selectProductInstructionMainByInstNum(instNum);
    }

    /**
     * 查询生产指令列表
     * 
     * @param productInstructionMain 生产指令
     * @return 生产指令
     */
    @Override
    public List<ProductInstructionMain> selectProductInstructionMainList(ProductInstructionMain productInstructionMain) {
        return productInstructionMainMapper.selectProductInstructionMainList(productInstructionMain);
    }

    /**
     * 新增生产指令
     * 
     * @param productInstructionMain 生产指令
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductInstructionMain(ProductInstructionMain productInstructionMain) {
        int rows = productInstructionMainMapper.insertProductInstructionMain(productInstructionMain);
        insertProductInstructionDetail(productInstructionMain);
        return rows;
    }

    /**
     * 修改生产指令
     * 
     * @param productInstructionMain 生产指令
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductInstructionMain(ProductInstructionMain productInstructionMain) {
        productInstructionMainMapper.deleteProductInstructionDetailByExcelServerRCID(productInstructionMain.getInstNum());
        insertProductInstructionDetail(productInstructionMain);
        return productInstructionMainMapper.updateProductInstructionMain(productInstructionMain);
    }

    /**
     * 批量删除生产指令
     * 
     * @param instNums 需要删除的生产指令主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductInstructionMainByInstNums(String[] instNums) {
        productInstructionMainMapper.deleteProductInstructionMainByInstNums(instNums);
        return productInstructionMainMapper.deleteProductInstructionMainByInstNums(instNums);
    }

    /**
     * 删除生产指令信息
     * 
     * @param instNum 生产指令主键
     * @return 结果
     */
    @Override
    public int deleteProductInstructionMainByInstNum(String instNum) {
        productInstructionMainMapper.deleteProductInstructionDetailByExcelServerRCID(instNum);
        return productInstructionMainMapper.deleteProductInstructionMainByInstNum(instNum);
    }

    /**
     * 新增生产指令信息
     * 
     * @param productInstructionMain 生产指令对象
     */
    public void insertProductInstructionDetail(ProductInstructionMain productInstructionMain) {
        List<ProductInstructionDetail> productInstructionDetailList = productInstructionMain.getProductInstructionDetailList();
        String instNum = productInstructionMain.getInstNum();
        if (StringUtils.isNotNull(productInstructionDetailList))
        {
            List<ProductInstructionDetail> list = new ArrayList<ProductInstructionDetail>();
            for (ProductInstructionDetail productInstructionDetail : productInstructionDetailList)
            {
                productInstructionDetail.setExcelserverrcid(instNum);
                list.add(productInstructionDetail);
            }
            if (list.size() > 0)
            {
                productInstructionMainMapper.batchProductInstructionDetail(list);
            }
        }
    }
}
